<script setup>
import { ref } from "vue";
import { userUpdatePassService } from "@/api/user/user";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores";

// 定义密码表单的初始状态
const pwdForm = ref({
  old_pwd: "",
  new_pwd: "",
  re_pwd: "",
});

/**
 * 验证新密码和旧密码不能相同
 * @param {Object} rule 验证规则对象
 * @param {string} value 新密码的值
 * @param {Function} cb 回调函数，验证通过或不通过时调用
 */
const checkOldSame = (rule, value, cb) => {
  if (value === pwdForm.value.old_pwd) {
    cb(new Error("原密码和新密码不能一样!"));
  } else {
    cb();
  }
};

/**
 * 验证新密码和确认密码是否相同
 * @param {Object} rule 验证规则对象
 * @param {string} value 确认密码的值
 * @param {Function} cb 回调函数，验证通过或不通过时调用
 */
const checkNewSame = (rule, value, cb) => {
  if (value !== pwdForm.value.new_pwd) {
    cb(new Error("新密码和确认再次输入的新密码不一样!"));
  } else {
    cb();
  }
};

// 定义表单的验证规则
const rules = {
  // 原密码规则
  old_pwd: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}$/,
      message: "密码长度必须是6-15位的非空字符串",
      trigger: "blur",
    },
  ],
  // 新密码规则
  new_pwd: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}$/,
      message: "密码长度必须是6-15位的非空字符串",
      trigger: "blur",
    },
    { validator: checkOldSame, trigger: "blur" },
  ],
  // 确认新密码规则
  re_pwd: [
    { required: true, message: "请再次确认新密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}$/,
      message: "密码长度必须是6-15位的非空字符串",
      trigger: "blur",
    },
    { validator: checkNewSame, trigger: "blur" },
  ],
};

// 获取表单引用
const formRef = ref();
// 获取路由实例
const router = useRouter();
// 获取用户状态管理实例
const userStore = useUserStore();
// 定义加载状态
let loading = ref(false);

/**
 * 提交表单，更新密码
 */
const onSubmit = async () => {
  loading.value = true;
  // 验证表单
  const valid = await formRef.value.validate();
  if (valid) {
    // 调用更新密码服务
    await userUpdatePassService(pwdForm.value);
    loading.value = false;
    // 成功提示，清除用户信息并跳转到登录页
    ElMessage({ type: "success", message: "更换密码成功" });
    userStore.setToken("");
    userStore.setUserInfo({});
    router.push("/login");
  }
};

// 重置表单
const onReset = () => {
  formRef.value.resetFields();
};
</script>
<template>
  <page-container title="重置密码">
    <el-row>
      <el-col :span="8">
        <el-form
          :model="pwdForm"
          :rules="rules"
          ref="formRef"
          label-width="100px"
          size="large"
        >
          <el-form-item label="原密码" prop="old_pwd">
            <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd">
            <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="onSubmit" type="primary" :loading="loading"
              >修改密码</el-button
            >
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </page-container>
</template>
